package com.engine.budget.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


/**
 * @author 祖梦杰
 * @date 2024/7/23 10:28
 * @description: 预算项目底表查询接口
 * @version 1.0
 */
public class GetYsxmdbCmd extends AbstractCommonCommand<Map<String,Object>> {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public GetYsxmdbCmd(User user, Map<String,Object> params) {
        this.user = user;
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }


    @Override
    public Map<String, Object> execute(CommandContext commandContext) {

        Map<String, Object> map = new HashMap<>();
        //4.定义SQL语句
        String sql="SELECT " +
                "uf.xmbm," +
                "uf.xmmc," +
                "uf.xmmblx," +
                "uf.hdnf," +
                "uf.xmzbrgh," +
                "uf.xmzbrmc," +
                "zx.bm," +
                "hm.departmentname," +
                "zx.yszrzxmc," +
                "zx.yszrzxbm," +
                "gz.dwzdgzmc," +
                "gz.dwzdgzbh," +
                "km.yskmmc," +
                "km.yskmbm" +
                " FROM " +
                " uf_ysxmdb uf " +
                " LEFT JOIN uf_ysxmdb_dt1 dt1 ON uf.id= dt1.mainid " +
                " LEFT JOIN uf_jccs_yszrzx zx ON uf.zz = zx.id " +
                " LEFT JOIN uf_dwzdgz gz ON gz.id = dt1.dwjzdgz " +
                " LEFT JOIN uf_jccs_yskm km ON km.id = dt1.yskm " +
                " LEFT JOIN hrmdepartment hm ON zx.bm = hm.id " +
                " WHERE " +
                " 1 = 1 ";

        String sqlTotal="SELECT COUNT" +
                "( 1 ) total " +
                " FROM " +
                " uf_ysxmdb uf " +
                " LEFT JOIN uf_ysxmdb_dt1 dt1 ON uf.id= dt1.mainid " +
                " LEFT JOIN uf_jccs_yszrzx zx ON uf.zz = zx.id " +
                " LEFT JOIN uf_dwzdgz gz ON gz.id = dt1.dwjzdgz " +
                " LEFT JOIN uf_jccs_yskm km ON km.id = dt1.yskm " +
                " LEFT JOIN hrmdepartment hm ON zx.bm = hm.id " +
                " WHERE " +
                " 1 = 1 ";


        ArrayList<String> sqlArrs = new ArrayList<>();
        ArrayList<String> sqlTotalArrs = new ArrayList<>();
        String workCode = (String)params.get("workCode");
        if(workCode!=null&&!workCode.isEmpty()){
            sql+=(" AND gz.dwzdgzbh = ?");
            sqlTotal+=(" AND gz.dwzdgzbh = ?");
            sqlArrs.add(workCode);
            sqlTotalArrs.add(workCode);
        }

        String projectCode = (String)params.get("projectCode");
        if(projectCode!=null&&!projectCode.isEmpty()){
            sql+=(" AND uf.xmbm = ? ");
            sqlTotal+=(" AND uf.xmbm = ? ");
            sqlArrs.add(projectCode);
            sqlTotalArrs.add(projectCode);
        }

        String respCenterCode = (String)params.get("respCenterCode");
        if(respCenterCode!=null && !respCenterCode.isEmpty()){
            sql+=(" AND zx.yszrzxbm = ? ");
            sqlTotal+=(" AND zx.yszrzxbm = ? ");
            sqlArrs.add(respCenterCode);
            sqlTotalArrs.add(respCenterCode);
        }

        String subjectCode = (String)params.get("subjectCode");
        if(subjectCode!=null && !subjectCode.isEmpty()){
            sql+=(" AND km.yskmbm = ? ");
            sqlTotal+=(" AND km.yskmbm = ? ");
            sqlArrs.add(subjectCode);
            sqlTotalArrs.add(subjectCode);
        }

        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sqlTotal,sqlTotalArrs);
        while (recordSet.next()) {
            map.put("total",recordSet.getString("total"));
        }




        int pageIndex = Util.getIntValue(params.get("pageIndex").toString(), 0);
        int pageSize = Util.getIntValue(params.get("pageSize").toString(), 0);
        if(pageIndex!=0&&pageSize!=0){
            sql+="LIMIT "+pageSize+" OFFSET "+((pageIndex-1)*pageSize);
        }else {
            sql+=("LIMIT 10 OFFSET 0 ");
        }

        logger.info("预算项目底表查询接口start...sql:"+sql);
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql,sqlArrs);
        //rs.executeUpdate();
        ArrayList<Map> data = new ArrayList<>();
        //7.处理结果，并获取数据
        while (rs.next()){
            HashMap<String, Object> mains = new HashMap<>();
            Map<String, Object> mainMap = new HashMap<>();
            mainMap.put("projectCode",rs.getString("xmbm"));
            mainMap.put("projectName",rs.getString("xmmc"));
            mainMap.put("respCenterCode",rs.getString("yszrzxbm"));
            mainMap.put("respCenterName",rs.getString("yszrzxmc"));
            mainMap.put("workCode",rs.getString("dwzdgzbh"));
            mainMap.put("workName",rs.getString("dwzdgzmc"));
            mainMap.put("subjectCode",rs.getString("yskmbm"));
            mainMap.put("subjectName",rs.getString("yskmmc"));
            mainMap.put("subjectType",rs.getString("xmmblx"));
            mainMap.put("year",rs.getString("hdnf"));
            mainMap.put("operateID",rs.getString("xmzbrgh"));
            mainMap.put("operateName",rs.getString("xmzbrmc"));
            mainMap.put("deptID",rs.getString("bm"));
            mainMap.put("deptName",rs.getString("departmentname"));
            mains.put("main",mainMap);
            data.add(mains);
        }
        map.put("data",data);
        return map;
    }
}
